package com.tantou.leetcode.easy;

/**
 * 二分查找
 * @author jiangweiyang
 * @created 2020/11/6 15:14
 **/
public class BinarySearch {

    public static void main(String[] args) {
        int[] input = {1, 3,3, 5, 6, 7, 8, 10, 15};
        System.out.println(findIdx(input, 3));
    }

    public static int findIdx(int[] input, int target) {
        int low = 0;
        int high = input.length - 1;

        while (low <= high) {
            int mid = (high + low) / 2;
            int guess = input[mid];
            if (guess == target) {
                return mid;
            } else if (guess > target) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }
}
